# 指定 arm64 架构
FROM --platform=linux/arm64 ubuntu:20.04

LABEL version="v0.1.0" description="file_parser" by="liuyang"

WORKDIR /preprocess_project

# 设置语言和 pip 镜像
ENV LANG=zh_CN.UTF-8
ENV LC_ALL=zh_CN.UTF-8
ENV UNO_PATH=/usr/lib/libreoffice/program
ENV PYTHONPATH=$UNO_PATH
ENV PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple

# 1. 使用国内镜像 + 安装依赖
RUN sed -i 's|http://archive.ubuntu.com|http://mirrors.aliyun.com|g' /etc/apt/sources.list && \
    sed -i 's|http://security.ubuntu.com|http://mirrors.aliyun.com|g' /etc/apt/sources.list && \
    sed -i 's|http://ports.ubuntu.com/ubuntu-ports|http://mirrors.aliyun.com/ubuntu-ports|g' /etc/apt/sources.list && \
    apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
    wget build-essential libssl-dev zlib1g-dev libncurses5-dev libncursesw5-dev \
    libreadline-dev libsqlite3-dev libffi-dev libbz2-dev liblzma-dev tk-dev \
    libgdbm-dev libc6-dev libgmp-dev libaio1 librocksdb-dev libc-ares2 \
    libgl1-mesa-glx libglib2.0-0 libgomp1 libreoffice libreoffice-script-provider-python \
    python3-uno unoconv fonts-wqy-microhei fonts-wqy-zenhei locales supervisor software-properties-common && \
    sed -i '/zh_CN.UTF-8/s/^# //g' /etc/locale.gen && locale-gen zh_CN.UTF-8 && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

# 2. 编译安装 Python 3.11
RUN wget https://www.python.org/ftp/python/3.11.9/Python-3.11.9.tgz && \
    tar -xzf Python-3.11.9.tgz && \
    cd Python-3.11.9 && \
    ./configure --enable-optimizations --with-ensurepip=install && \
    make -j$(nproc) && make altinstall && \
    cd .. && rm -rf Python-3.11.9 Python-3.11.9.tgz

# 3. 构建字体缓存
RUN fc-cache -fv

# 4. 安装 Python 依赖
COPY requirements.txt ./
RUN pip3.11 install --no-cache-dir --upgrade pip && \
    pip3.11 install --no-cache-dir -r requirements.txt

# 5. 下载 PaddleOCR 模型
RUN mkdir -p /root/.paddleocr/whl/det/ch/ch_PP-OCRv4_det_infer/ \
         /root/.paddleocr/whl/rec/ch/ch_PP-OCRv4_rec_infer/ \
         /root/.paddleocr/whl/cls/ch_ppocr_mobile_v2.0_cls_infer/ && \
    wget -q -O /root/.paddleocr/whl/det/ch/ch_PP-OCRv4_det_infer/ch_PP-OCRv4_det_infer.tar \
         https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_det_infer.tar && \
    wget -q -O /root/.paddleocr/whl/rec/ch/ch_PP-OCRv4_rec_infer/ch_PP-OCRv4_rec_infer.tar \
         https://paddleocr.bj.bcebos.com/PP-OCRv4/chinese/ch_PP-OCRv4_rec_infer.tar && \
    wget -q -O /root/.paddleocr/whl/cls/ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v2.0_cls_infer.tar \
         https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar && \
    tar -xf /root/.paddleocr/whl/det/ch/ch_PP-OCRv4_det_infer/ch_PP-OCRv4_det_infer.tar \
         -C /root/.paddleocr/whl/det/ch/ch_PP-OCRv4_det_infer/ && \
    tar -xf /root/.paddleocr/whl/rec/ch/ch_PP-OCRv4_rec_infer/ch_PP-OCRv4_rec_infer.tar \
         -C /root/.paddleocr/whl/rec/ch/ch_PP-OCRv4_rec_infer/ && \
    tar -xf /root/.paddleocr/whl/cls/ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v2.0_cls_infer.tar \
         -C /root/.paddleocr/whl/cls/ch_ppocr_mobile_v2.0_cls_infer/ && \
    rm -f /root/.paddleocr/whl/det/ch/ch_PP-OCRv4_det_infer/*.tar \
          /root/.paddleocr/whl/rec/ch/ch_PP-OCRv4_rec_infer/*.tar \
          /root/.paddleocr/whl/cls/ch_ppocr_mobile_v2.0_cls_infer/*.tar

# 6. 复制项目代码
COPY . .

# 设置启动命令
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8101"]
